package com.zhbcm.socket.server;

import com.zhbcm.socket.handler.TimeServerHandler;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author 朝花不迟暮
 * @version 1.0
 * @date 2021/4/23 20:22
 */
public class SocketServerDemo
{
    public static void main(String[] args)
    {
        ServerSocket serverSocket = null;
        try
        {
            serverSocket = new ServerSocket(18080);
            while (true)
            {
                Socket socket = serverSocket.accept();
                //每次接收到一个新的客户端连接，启动一个新的线程来处理
                new Thread(new TimeServerHandler(socket)).start();
            }
        } catch (IOException e)
        {
            e.printStackTrace();
        } finally
        {
            try
            {
                assert serverSocket != null;
                serverSocket.close();
            } catch (IOException e)
            {
                e.printStackTrace();
            }
        }
    }
}
